<template>
  <div>
    <van-datetime-picker
      v-model="currentDate"
      type="date"
      title="选择年月日"
      :min-date="minDate"
      :max-date="maxDate"
      @cancel="$emit('close')"
      @confirm="onConfirm"
    />
  </div>
</template>

<script>
import dayJs from 'dayjs'
import { updateUser } from '@/api/user.js'
import { Toast } from 'vant'

export default {
  props: {
    value: {
      type: String,
      required: true
    }
  },
  data () {
    return {
      currentDate: new Date(this.value),
      minDate: new Date('1900-01-01'),
      maxDate: new Date()
    }
  },

  created () {},

  methods: {
    async  onConfirm (val) {
      const formatTime = dayJs(val).format('YYYY-MM-DD')
      try {
        await updateUser({
          birthday: formatTime
        })
        this.$emit('close')
        this.$emit('input', formatTime)
        Toast.success('更新成功')
      } catch (err) {
        Toast.fail('更新失败')
      }
    }
  }
}
</script>

<style scoped lang="less"></style>
